images: down
	mkdir -p ./fabric-x86_64-1.0.0
	docker load -i ./fabric-x86_64-1.0.0/fabric-ca.tar
	docker load -i ./fabric-x86_64-1.0.0/fabric-javaenv.tar
	docker load -i ./fabric-x86_64-1.0.0/fabric-peer.tar 
	docker load -i ./fabric-x86_64-1.0.0/fabric-ccenv.tar 
	docker load -i ./fabric-x86_64-1.0.0/fabric-kafka.tar 
	docker load -i ./fabric-x86_64-1.0.0/fabric-tools.tar 
	docker load -i ./fabric-x86_64-1.0.0/fabric-couchdb.tar 
	docker load -i ./fabric-x86_64-1.0.0/fabric-orderer.tar 
	docker load -i ./fabric-x86_64-1.0.0/fabric-zookeeper.tar

	docker tag a15c59ecda5b  hyperledger/fabric-ca:latest
	docker tag 8948126f0935  hyperledger/fabric-javaenv:latest
	docker tag 6830dcd7b9b5  hyperledger/fabric-peer:latest
	docker tag 7182c260a5ca  hyperledger/fabric-ccenv:latest
	docker tag dbd3f94de4b5 hyperledger/fabric-kafka:latest
	docker tag 0403fd1c72c7 hyperledger/fabric-tools:latest
	docker tag 2fbdbf3ab945 hyperledger/fabric-couchdb:latest
	docker tag e317ca5638ba hyperledger/fabric-orderer:latest
	docker tag e545dbf1c6af hyperledger/fabric-zookeeper:latest

clean_images: down
	sh fabric.sh

FABRIC_SRC_DIR = /opt/gopath/src/github.com/hyperledger/
release: clean
	mkdir -p $(FABRIC_SRC_DIR) && cp -rf ./fabric-1.0.0.zip $(FABRIC_SRC_DIR)
	cd $(FABRIC_SRC_DIR) && unzip fabric-1.0.0.zip && mv fabric-1.0.0 fabric 
	rm -rf $(FABRIC_SRC_DIR)/fabric/examples/e2e_cli && cp -rf ./e2e_cli $(FABRIC_SRC_DIR)/fabric/examples/
	chmod -R +x $(FABRIC_SRC_DIR)/fabric/examples/e2e_cli/*
	cd $(FABRIC_SRC_DIR)/fabric && make release

restart:
	cd $(FABRIC_SRC_DIR)/fabric/examples/e2e_cli && ./network_setup.sh restart

down:
	if [ -f $(FABRIC_SRC_DIR)/fabric/examples/e2e_cli/network_setup.sh ]; then cd $(FABRIC_SRC_DIR)/fabric/examples/e2e_cli && ./network_setup.sh down; else echo "No network_setup.sh"; fi;

test:
	docker exec -it cli bash && \
	peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}' && \
	peer chaincode query -C mychannel -n mycc -c '{"Args":["query","b"]}' && \
	peer chaincode invoke -o orderer.example.com:7050  --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem  -C mychannel -n mycc -c '{"Args":["invoke","a","b","20"]}' && \
	peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}' && \
	peer chaincode query -C mychannel -n mycc -c '{"Args":["query","b"]}'

clean: down
	rm -rf $(FABRIC_SRC_DIR)/*
	
.PHONY: images clean_images release restart down test clean
